var express = require('express');
var router = express.Router();
var user = require("../model/user/user");

/* GET users listing. */
router.get('/', function(req, res, next) {
    res.send('respond with a resource');
});
router.get('/login', function(req, res, next) {
    res.render("users/login");
});

router.get("/manager", function(req, res, next) {
    user.list().then(function(data) { //data是用户的列表
        res.render("users/manager", {
            list: data
        });
    }, function(data) {
        //请求失败
    })

})

router.get('/apilogin', function(req, res, next) {
    var username = req.query.username;
    var userpwd = req.query.userpwd;
    user.login(username, userpwd).then(function(result) {
        req.session.userid = result.id;
        req.session.username = username;
        res.json(result);
    }, function(data) {
        //console.log(data);
        res.json(data);
    })
});
router.get("/apiadd", function(req, res, next) {

    var username = req.query.username;
    var userpwd = req.query.userpwd;
    var userinfo = {
        "username": username,
        "userpwd": userpwd,
        "usertype": 2
    }
    user.add(userinfo).then(function(result) {
        res.json(result);
    });
    //console.log("调用成功");
})
router.get("/apidelete", function(req, res, next) {
    var id = req.query.id;
    user.deletebyid(id).then(function(result) {
        res.json(result);
    })
})

module.exports = router;